var maxItems = 50;
var maxTweets = 10;		// max amount of tweets to show in a stream
var maxBlogs = 10; 		// max amount of blogs to show in a stream
var maxComments = 10; 	// max amount of comments to show in a stream
var HOME = 'home';
var COURSE = 'course';
var TODO = 'todo';
var JSONURL = 'http://mumesint.appspot.com/api/entity?f=export';

// function to initialize the page
var init = function() {
	// showing the home tab on initializing
	showPage(HOME, null, null);
	$('#leaderboard').hide();
	$('#todo').hide();
	
	// event listeners for the navbar
	$('#homebtn').click(function() {
		showPage(HOME, null, null);
	});
	$('#todobtn').click(function() {
		showPage(TODO, null, null);
	});
	$('.course').click(function() {
		showPage(COURSE, event.currentTarget.id, null);
	});
}

/* 
 * function to fill in the primary content 
 * parameters declare how the page will be configured
 */
var showPage = function(entity, course, type) {
	// TODO get the students name and set the link of the score to the proper profile
	// A homepage and course both need a stream
	if (entity == HOME || entity == COURSE) {
		$('.content-primary').empty(); 					// clear the content
		$('.content-primary').html($('#home').html()); 	// put basic 'home' content
		if (entity == HOME) {
			$('#courseinfo').hide(); 					// don't show 3 icons
			loadStream(null, null, null); 				// load the stream without filters
		} else {
			$('#courseinfo').show(); 					// show the 3 icons
			// TODO set proper links from all the icons
			$('#coursetitle').html(course); 			// show the title
			loadStream(null, null, null); 			// load the stream with a specific course  filter
		}
		$('#score').listview();							// update score for proper jQuery layout
		$('#list-stream').listview(); 					// update stream for proper jQuery layout
	} else if (entity == TODO) {
		$('.content-primary').empty(); 					// clear the content
		$('.content-primary').html($('#todo').html()); 	// put todo content
		// TODO generate todolist
	}
}

/*
 * load a page with corresponding 3 charasteristics student = student who's
 * using the app and logged in courseFiler = only loads the page with items
 * corresponding to this course. Null for all courses typeFilter = only shows
 * the types in the stream of this type. Null for all types
 */
var loadStream = function(student, courseFilter, typeFilter) {
	//TODO switch statement?
	if (typeFilter == null){
		showAll(courseFilter);
	} else if(typeFilter == 'TWEET')
		showTweets(courseFilter);
	else if(typeFilter == 'BLOG'){
		showBlogs(courseFilter);
		showComment(courseFilter);
	} else if(typeFilter == 'QUESTION'){
		showQuestion(coursefilter);
		showAnswer(coursefilter);
	} else if(typeFilter == 'TODO')
		showTODO(student);
	else if(typeFilter == 'ACHIEVEMENT')
		showAchievement(student);
}

var calculateNewHtml = function(type, guid, link, author, text, datestamp) {
	var htm = '';
	htm += '<li id="'
		+ guid
		+ '"><a href= "'
		+ link
		+ '" rel="external"><img src = "img/' + type + '.png" heigt="50" width="50"></img><h3>'
		+ author
		+ '</h3><p><strong> '
		+ text
		+ '</strong></p><p class="ui-li-aside"><strong>'
		+ datestamp
		+ '</strong>PM</p></a></li>';
	return htm;
}

var showAll = function(courseFilter) {
	$.getJSON(JSONURL, function (data){
		var htm = '';
		var nbItems = data.total_results - 1;				//aantal items in json file
		var results = [ nbItems ];
		$.each(data.results, function(i, result) {
				results[i] = result;						//vul de results-array met json-objecten
		});
		$.each(results, function(i, result) {
			if(result.entitytype == 'Tweet')
				htm += calculateNewHtml('twitter', result.guid, result.link, result.author, result.text, result.datestamp);
			else if(result.entitytype == 'Comment')
				htm += calculateNewHtml('blog', result.guid, result.link, result.author, result.text, result.datestamp);
			else if(result.entitytype == 'Blog')
				htm += calculateNewHtml('blog', result.guid, result.link, result.author, result.text, result.datestamp);
			
			if (i == maxItems - 1)
				return false;
		});
		$('#list-stream').append(htm);
		$('#list-stream').listview('refresh');
	});
}

var showTweets = function(courseFilter) {
	$.getJSON(JSONURL + '&ent=Tweet', function (data){
		var htm = '';
		var nbTweets = data.total_results - 1;				//aantal tweets in json file
		var results = [ nbTweets ];
		$.each(data.results, function(i, result) {
				results[i] = result;						//vul de results-array met json-objecten
		});
		$.each(results, function(i, result) {
			htm += calculateNewHtml('twitter', result.guid, result.link, result.author, result.text, result.datestamp);
			if (i == maxTweets - 1)
				return false;
		});
		$('#list-stream').append(htm);
		$('#list-stream').listview('refresh');
	});
}

var showBlogs = function(courseFilter) {
	$.getJSON(JSONURL + "/api/entries/blog", function(data) {
		var htm = '';
		var nbBlogs = data.total_results - 1;			// aantal blogs in json-file
		var results = [ nbBlogs ];
		$.each(data.results, function(i, result) {
			results[i] = result;						//vul de results-array met json-objecten
		});
		$.each(results,	function(i, result) {
			htm += calculateNewHtml('blog', result.guid, result.link, result.author, result.text, result.datestamp);
			if (i == maxBlogs - 1)
				return false;
		});
		$('#list-stream').append(htm);
		$('#list-stream').listview('refresh');
	});
}

var showComment = function(courseFilter) {
	$.getJSON(JSONURL + "/api/entries/comment", function(data) {
	var htm = '';
	var nbComments = data.total_results - 1;
	var results = [ nbComments ];						//aantal comments in json-file
	$.each(data.results, function(i, result) {
		results[i] = result;							//vul de results-array met json-objecten
	});
	$.each(results, function(i, result) {
		htm += calculateNewHtml('blog', result.guid, result.link, result.author, result.text, result.datestamp);
		if (i == maxComments - 1)
			return false;
	});
	$('#list-stream').append(htm);
	$('#list-stream').listview('refresh');
	});
}

var showQuestion = function(courseFilter) {
	$.getJSON(JSONURL + "/api/entries/question", function(data) {
		var htm = '';
		var nbQuestion = data.total_results - 1;
		var results = [ nbQuestion ];						//aantal comments in json-file
		$.each(data.results, function(i, result) {
			results[i] = result;							//vul de results-array met json-objecten
		});
		$.each(results, function(i, result) {
			htm += calculateNewHtml('question', result.guid, result.link, result.author, result.text, result.datestamp);
		});
		$('#list-stream').append(htm);
		$('#list-stream').listview('refresh');
		});
}

var showAnswer = function(courseFilter) {
	// TODO
}

var showTODO = function(student) {
	// TODO
}

var showAchievement = function(student) {
	// TODO
}

//function to save an entity
var saveQuestion = function(data) {
	 alert("question");
	// creating the data object to be sent to backend
	 var data=new Array();
	 data[data.length] = new param('user', 'test');
	// collecting the field values from the form
	 var formEleList = $('#form-question').serializeArray();
	 data[data.length]=new param('question',formEleList[0].value); //copy questionform
	 //setting action as PUT
	 data[data.length]=new param('action','PUT');
	 //making the ajax call
	 $.ajax({
			url : '/homepage',
			type : "POST",
			data:data,
			success : function() {
				alert("succes");
			}
		});
}